<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/header.php");
?>


<section>
    
    <SCRIPT>
function reload(form){
var val=form.onderdeel.options[form.onderdeel.options.selectedIndex].value;
self.location='probleemtoevoegen.php?screenid='+<?php echo $_GET["screenid"]; ?>+'&roomid='+<?php echo $_GET["roomid"]; ?>+'&onderdeel=' + val ;
}

function handleClick(form, myRadio) {
var val=form.onderdeel.options[form.onderdeel.options.selectedIndex].value;
self.location='probleemtoevoegen.php?screenid='+<?php echo $_GET["screenid"]; ?>+'&roomid='+<?php echo $_GET["roomid"]; ?>+'&onderdeel=' + val +'&problem='+myRadio.value;
}

</script>
<?php
        
        function get_children($parent, $level = 1)
        {

        $sql5 = "SELECT * FROM itemproblem WHERE ParentId = ".$parent."";
        $result5 = mysql_query($sql5) or die (mysql_error());

        if(mysql_num_rows($result5) > 0)
        {
            echo '<ul>';

            while($row5 = mysql_fetch_array($result5))
            {
                $sql9 = "SELECT * FROM itemproblem WHERE ParentId = ".$row5['Id']."";
                $result9 = mysql_query($sql9) or die (mysql_error());
                if(mysql_num_rows($result9) > 0)
                {
                    
                    echo "<li>".$row5['Name']."</li>";
                }
                else
                {   
                    if (isset($_GET['problem']))
                    {

                        if ($row5['Id'] == $_GET['problem'])
                        {
                            echo "<li><input type=\"radio\" name=\"probleem\" checked onchange=\"handleClick(this.form, this);\" value=\"".$row5['Id']."\">".$row5['Name']."</li>";
                        }
                        else
                        {
                           echo "<li><input type=\"radio\" name=\"probleem\" onchange=\"handleClick(this.form, this);\" value=\"".$row5['Id']."\">".$row5['Name']."</li>";
                        }
                    }
                    else
                    {
                        echo "<li><input type=\"radio\" name=\"probleem\" onchange=\"handleClick(this.form, this);\" value=\"".$row5['Id']."\">".$row5['Name']."</li>";
                    }
                }
                get_children($row5['Id'], $level+1);
            }
            echo '</ul>';
            }
        }
?>
    
<hgroup>
<h1>Probleem toevoegen</h1>
</hgroup>

<?php if(!isset($_POST['Submit'])) {
?>
<form  name="kamerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
    <tr>
        <td>Kamer:</td>
        <td>
            <?php
                $sql1 = "SELECT * from roomitem WHERE Id=".$_GET['roomid']."";
                $result1 = mysql_query($sql1) or die (mysql_error());
                while($row1 = mysql_fetch_array($result1))
                {
                    $sql2 = "SELECT * from roomtype WHERE Id=".$row1['RoomTypeId']."";
                    $result2 = mysql_query($sql2) or die (mysql_error());
                    while($row2 = mysql_fetch_array($result2))
                    {
                        echo "<input name=\"kamer\" value=\"".$row2['Name']."\" disabled type=\"text\">";
                    }
                }
                
                
            ?>
        </td>
    </tr>
    <tr>
        <td>Onderdeel:</td>
        <td>
            <select onchange="reload(this.form)" name="onderdeel">
            <option value="0">Kies een onderdeel</option>
            <?php
                $sql3 = "SELECT * from roomitem WHERE RoomTypeId=".$_GET['roomid']."";
                $result3 = mysql_query($sql3) or die (mysql_error());
                while($row3 = mysql_fetch_array($result3))
                {
                    if (isset($_GET['onderdeel']))
                    {
                        if ($_GET['onderdeel'] == $row3['Id'])
                        {
                            echo "<option selected value=\"".$row3['Id']."\">".$row3['Name']."</option>";
                        }
                        else
                        {
                            echo "<option value=\"".$row3['Id']."\">".$row3['Name']."</option>";
                        }
                    }
                    else
                    {
                        echo "<option value=\"".$row3['Id']."\">".$row3['Name']."</option>";
                    }
                }
            ?>
            </select>
        </td>
    </tr>
    <?php
    
    if (isset($_GET['roomid']) && isset($_GET['onderdeel']))
            {
        ?>
    <tr>
        <td>Onderdeel-probleem:</td>
        <td>
            <?php
            
                $sql4 = "SELECT * FROM itemproblem where RoomItemId=".$_GET['onderdeel']."";
                $result4 = mysql_query($sql4) or die (mysql_error());

                #for avoiding some errors
                if(mysql_num_rows($result4) > 0)
                {
                #start the list
                echo "<ul>";

                while($row4 = mysql_fetch_array($result4))
                {
                    if ($row4['ParentId'] == -1)
                    {
                        if (isset($_GET['problem']))
                        {
                           if ($row4['Id'] == $_GET['problem'])
                            {
                                echo "<li>".$row4['Name']."</li>";
                            }
                            else
                            {
                               echo "<li>".$row4['Name']."</li>";
                            } 
                        }
                        else
                        {
                           echo "<li>".$row4['Name']."</li>";
                        } 

                        get_children($row4['Id']);
                    }
                }
                #end the list
                echo "</ul>";
                #some message if the database is empty
                }
                else
                {
                    echo 'Er zijn nog geen problemen toegevoegd voor dit onderdeel';
                }

                #clear the memory
                mysql_free_result($result4);
            }
            ?>
        </td>
    </tr>
    
    
    <tr>
        <td>Extra tekst:</td>
        <td>
            <textarea name="comment" cols="25" rows="7"></textarea>
        </td>
    </tr>
    <?php
        if (isset($_GET['problem']))
        {
    ?>
    <?php
    
        $sql6 = "SELECT * from itemproblem WHERE Id=".$_GET['problem']."";
        $result6 = mysql_query($sql6) or die (mysql_error());
        while($row6 = mysql_fetch_array($result6))
        {
            $Value = $row6['HasValue'];
        }
        if ($Value == "T")
        {
    ?>
    <tr>
        <td>Waarde:</td>
        <td>
            <input placeholder="Waarde" name="value" type="text">
        </td>
    </tr>
    <?php
        }

    }
    ?>
    <tr>
        <td><input type="submit" name="Submit" value="Toevoegen">
            <input type="hidden" name="screening" value="<?php echo $_GET['screenid'] ?>" />
            <input type="hidden" name="problem" value="<?php if (isset($_GET['problem'])) { echo $_GET['problem']; } ?>" />
            <input type="hidden" name="roomid" value="<?php echo $_GET['roomid'] ?>" />
        </td>
        <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
    </tr>
    
</table>
</form>
<?php
} // einde isset submit
else 
{	
    
    $query9 = "SELECT * FROM screeningroom WHERE RoomTypeId=".$_POST['roomid']." AND ScreeningVersionId=".$_POST['screening']."";
    $result9 = mysql_query($query9) or die (mysql_error());
    while ($row9 = mysql_fetch_array($result9))
    {
        $query10 = "SELECT * FROM screeningitem WHERE ScreeningRoomId = ".$row9['Id']." AND RoomItemId = ".$_POST['onderdeel']."";
        $result10 = mysql_query($query10) or die (mysql_error());
        while ($row10 = mysql_fetch_array($result10))
        {
            $id = $row10['Id'];
        }
    }
    
    $sql8 = "SELECT * from itemproblem WHERE Id=".$_POST['problem']."";
    $result8 = mysql_query($sql8) or die (mysql_error());
    while($row8 = mysql_fetch_array($result8))
    {
        $Value = $row8['Value'];
    }
    if ($Value == "T")
    {
        mysql_query("INSERT INTO screeningproblem (ScreeningItemId, ItemProblemId, Value) VALUES ('".$id."','".$_POST['problem']."','".$_POST['value']."')");
        echo "Het probleem is toegevoegd.<br>";
        echo "Klik <a href=\"screening.php?id=".$_POST['screening']."\">hier</a> om terug te gaan naar het overzicht.";
    }
    else
    {
        mysql_query("INSERT INTO screeningproblem (ScreeningItemId, ItemProblemId) VALUES ('".$id."','".$_POST['problem']."')");
        echo "Het probleem is toegevoegd.<br>";
        echo "Klik <a href=\"screening.php?id=".$_POST['screening']."\">hier</a> om terug te gaan naar het overzicht.";
    }
}
?>    

</section>
<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>